﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Earworm.Web.DBM;

namespace Earworm.Web.Admin
{
    public partial class AdminLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            CustomValidator val = new CustomValidator();
            string err = "";
            EarwormDataContext ear = new EarwormDataContext();
            var i = ear.TestLogin(txtUserName.Text, txtPassword.Text, ref err);

            if (i.ReturnValue.ToString() == "1")
            {
                var j = from a in ear.Users
                        where a.UserName == txtUserName.Text
                        select new { a.Admin };

                if (j.First().Admin)
                {
                    Response.Redirect("~/Admin/AdminHome.aspx");
                }
                else
                {
                    val.IsValid = false;
                    val.ErrorMessage = "This is not a valid admin account";
                    this.Page.Validators.Add(val);
                    txtPassword.Text = "";
                }
            }
            else
            {
                val.IsValid = false;
                val.ErrorMessage = err;
                this.Page.Validators.Add(val);
                txtPassword.Text = "";
            }
        }
    }
}